/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.dao.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Holds the following associated variable:
 * <ul>
 * <li>sessionFactory(SessionFactory)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [16.07.2007] Created (Erik Nijkamp)<br>
 *  - [05.04.2009] Fixing getSessionFactory (Ulrich Staudinger)<br>
 *
 *  @author Erik Nijkamp
 */
public class HibernateUtil {
	/**
	 * private static SessionFactory sessionFactory;
	 */
	private static SessionFactory sessionFactory;

	/**
	 * Returns the associated sessionFactory(SessionFactory), initializing it from a resource named <strong>hibernate.cfg.xml</strong> if it is null
	 * @return the default sessionFactory
	 */
	public static SessionFactory getSessionFactory() {
		if(sessionFactory == null) {
			try {
				// Create the SessionFactory
				sessionFactory = new Configuration().configure().buildSessionFactory();
			} catch (Throwable ex) {
				throw new ExceptionInInitializerError(ex);
			}			
		}
		return sessionFactory;
	}
	
	/**
	 * returns the session factory from the configuration passed in.<br/>
	 * Returns the associated sessionFactory(SessionFactory), initializing it from the given config(Configuration) if necessary
	 * @param config
	 * @return
	 */
	public static SessionFactory getSessionFactory(Configuration config) {
		if(sessionFactory == null) {
			try {
				// Create the SessionFactory
				sessionFactory = config.buildSessionFactory();
			} catch (Throwable ex) {
				throw new ExceptionInInitializerError(ex);
			}			
		}
		return sessionFactory;
	}

}
